﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DCSLibrary;
using Utils.DatabaseUtils;
using Utils.DC.Web.UI;

public partial class Admin_Form_ProductNews_Add : System.Web.UI.Page
{
    NewsUI objNews = new NewsUI();
    DataSet dsNews;

    protected void Page_Load(object sender, EventArgs e)
    {
        dsNews = objNews.GetAll();
        if (!IsPostBack)
        {
            BindNews();
            if (Request.QueryString["id"] != null && Request.QueryString["id"].Trim() != "")
            {
                try
                {
                    LoadEditData(Convert.ToInt32(Request.QueryString["id"]));
                }
                catch (FormatException fe)
                {
                }
            }
        }
    }

    private void LoadEditData(int product)
    {
        txtProductID.Value = product.ToString();
        DataSet ds = new ProductUI().GetRelateNews(product);
        if (TableLib.HasData(ds))
        {
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                lstChooseNews.Items.Add(new ListItem(row["NewsTitle"].ToString(),row["NewsID"].ToString()));
            }
        }
    }

    protected void News_Add(object sender, EventArgs e)
    {
        try
        {
            LinkButton button = (LinkButton)sender;
            string title = dsNews.Tables[0].Select("NewsID = " + button.CommandArgument)[0]["NewsTitle"].ToString();
            ListItem item = new ListItem(title, button.CommandArgument);
            if(!lstChooseNews.Items.Contains(item))
                lstChooseNews.Items.Add(item);
        }
        catch (Exception ex)
        {
        }
    }

    protected void btnCreate_Click(object sender, EventArgs e)
    {
        //bien page-alias
        try
        {

            int result = new ProductUI().UpdateRelateNews(Convert.ToInt32(txtProductID.Value),GetListNews());

            if (result > 0)
            {
                //Response.Write(messageBox.Alert(MessageBox.MESSAGE_ADD_SUCCESSFULLY));
                lblMessage.Text = MessageBox.MESSAGE_UPDATE_SUCCESSFULLY;
                ReInsertData();
            }
            else
            {
                //Response.Write(messageBox.Alert(MessageBox.MESSAGE_ADD_NOT_SUCCESSFULLY));
                lblMessage.Text = MessageBox.MESSAGE_UPDATE_NOT_SUCCESSFULLY;
            }
        }
        catch (Exception ex)
        {
            //Response.Write(messageBox.Alert(MessageBox.MESSAGE_DUPLICATE_DATA));
            lblMessage.Text = MessageBox.MESSAGE_CONFILCT_DATA;
        }
    }

    private string GetListNews()
    {
        string list = "";
        if(lstChooseNews.Items.Count > 0)
        {
            foreach(ListItem item in lstChooseNews.Items)
            {
                list += item.Value + ",";
            }

            list = list.Substring(0,list.Length - 1);
        }
        return list;
    }

    protected void btnReset_Click(object sender, EventArgs e)
    {
        ReInsertData();
    }

    private void ReInsertData()
    {
        lstChooseNews.Items.Clear();
    }


    protected void btnDelete_Click(object sender, EventArgs e)
    {
        if (lstChooseNews.SelectedItem != null)
        {
            lstChooseNews.Items.Remove(lstChooseNews.SelectedItem);
        }
    }

    private void BindNews()
    {
        
        if (TableLib.HasData(dsNews))
        {
            DataTable dt = TableLib.CreateTable(dsNews.Tables[0], "NewsType = 'News'");
            grd.DataSource = dt;
            grd.DataBind();
        }
    }       
}
